import json
import random
import string
from datetime import date
from utils import respond_code
from utils.db import load_json_data
from utils.respond_code import StudentIdException


class Person:
    def __init__(self, name: str, age: int):
        self.name = name
        self.age = age

    @property
    def age(self):
        return self._age

    @age.setter
    def age(self, value):
        if not (16 <= value <= 40):
            raise ValueError("年龄需在16-40岁之间")
        self._age = value

    def introduce(self):
        return f"我是{self.name}, 年龄{self.age}"


class Student(Person):
    """ 普通学生类 """

    def __init__(self, name: str, age: int, student_id: str, **kwargs):
        super().__init__(name, age)
        # 学号
        self._student_id = student_id
        # 成绩表
        self._scores = kwargs.get('scores', {})
        # 已选课程
        self._courses = kwargs.get('courses', [])

    @property
    def student_id(self):
        """学号"""
        return self._student_id

    @student_id.setter
    def student_id(self, value):
        if not value.startswith('YA'):
            raise StudentIdException()
        self._student_id = value

    @property
    def scores(self):
        """成绩"""
        return self._scores

    @property
    def courses(self):
        return self._courses

    def __str__(self):
        return json.dumps(
            {
                "name": self.name,
                "age": self.age,
                "student_id": self.student_id,
                "scores": self.scores,
                "courses": self.courses
            },
            indent=4
        )

    @property
    def to_dict(self):
        return {
            "name": self.name,
            "age": self.age,
            "student_id": self.student_id,
            "scores": self.scores,
            "courses": self.courses
        }
